package controllers
{
	import errors.UIError;
	import errors.UIWarning;
	
	import mx.controls.Alert;
	
	public class UIErrorController
	{
		public static const RESTErrors:Object = {
			"login_failure": "Login failed."
		};
		
		public static const RESTWarnings:Object = {
		};
		
		public static const UIErrors:Object = {
			"login_missing_fields": 			"Username and password required",
			"register_must_accept_terms": 		"Please accept the terms of use",
			"register_missing_fields": 			"Please fill in all the fields"
		};
		
		public static const UIWarnings:Object = {
		};
		
		private static function getMessage(table:Object, errorCode:String) : String {
			if(typeof table[errorCode] != 'undefined') {
				return table[errorCode];
			} else {
				return "Unknown error message with code \"" + errorCode + "\""; 
			}
		}
		
		public static function handleUIError(error:UIError) : void {
			Alert.show(getMessage(UIErrors, error.code), "User error");
		}
		
		public static function handleUIWarning(error:UIWarning) : void {
			Alert.show(getMessage(UIWarnings, error.code), "User warning");
		}
		
		public static function handleUIErrorCode(errorCode:String) : void {
			Alert.show(getMessage(UIErrors, errorCode), "User error");
		}
		
		public static function handleUIWarningCode(errorCode:String) : void {
			Alert.show(getMessage(UIWarnings, errorCode), "User warning");
		}
		
		public static function handleRESTWarningCode(errorCode:String) : void {
			Alert.show(getMessage(RESTWarnings, errorCode), "Communication error");
		}
		
		public static function handleRESTErrorCode(errorCode:String) : void {
			Alert.show(getMessage(RESTErrors, errorCode), "Communication warning");
		}
	}
}